package com.ABdolphin.forum.controller;

import com.ABdolphin.forum.common.AppConfig;
import com.ABdolphin.forum.common.AppResult;
import com.ABdolphin.forum.common.ResultCode;
import com.ABdolphin.forum.model.Message;
import com.ABdolphin.forum.model.User;
import com.ABdolphin.forum.services.IMessageService;
import com.ABdolphin.forum.services.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.constraints.NotNull;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "站内信接口")
@RestController
@RequestMapping("/message")
public class MessageController {
    @Resource
    private IMessageService messageService;
    @Resource
    private IUserService userService;
    @PostMapping("/send")
    @Operation(summary = "发送站内信")
    public AppResult send(HttpServletRequest request,
                          @Parameter(name = "receiveUserId",description = "接受者id")@RequestParam("receiveUserId")@NotNull Long receiveUserId,
                          @Parameter(name = "content",description = "内容")@RequestParam("content")@NotNull String content){
        //1,用户的状态,不能是禁言的状态
        HttpSession session = request.getSession(false);
        User user=(User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //2,不能给自己发站内信
        if (user.getId().equals(receiveUserId)){
            return AppResult.failed("不能给自己发站内信");
        }
        //3,校验接收者是否存在
        User receiveUser = userService.selectById(receiveUserId);
        if (receiveUser==null||receiveUser.getDeleteState()==1){
            return AppResult.failed("接受者状态异常");
        }
        Message message=new Message();
        message.setPostUserId(user.getId());
        message.setReceiveUserId(receiveUserId);
        message.setContent(content);
        messageService.create(message);
        return AppResult.success("发送成功");
    }
    @Operation(summary = "获取未读数")
    @GetMapping("/getUnreadCount")
    public AppResult<Integer> getUnreadCount (HttpServletRequest request){
        HttpSession session = request.getSession(false);
        User user=(User) session.getAttribute(AppConfig.USER_SESSION);
        Integer count = messageService.selectUnreadCount(user.getId());
        return AppResult.success(count);
    }
    @Operation(summary = "获取用户所有的站内信")
    @GetMapping("/getAll")
    public AppResult<List<Message>> getAll (HttpServletRequest request){
        HttpSession session = request.getSession(false);
        User user=(User) session.getAttribute(AppConfig.USER_SESSION);
        List<Message> messages = messageService.selectByReceiveId(user.getId());
        return AppResult.success(messages);
    }

    @Operation(summary = "更新为已读")
    @PostMapping("/markRead")
    public AppResult<List<Message>> markRead (HttpServletRequest request,
                                              @Parameter(name = "id",description = "站内信id")@RequestParam("id")@NotNull Long id){
        //根据id查询站内信
        Message message = messageService.selectById(id);
        //站内信是否存在
        if (message==null||message.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
        //站内信是不是自己的
        HttpSession session = request.getSession(false);
        User user=(User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getId()!=message.getReceiveUserId()){
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        messageService.updateStateById(id,(byte)1);
        return AppResult.success();
    }
    @Operation(summary = "回复站内信")
    @PostMapping("/reply")
    public AppResult reply(HttpServletRequest request,
                           @Parameter(name = "repliedId",description = "要回复的站内信id")@RequestParam("repliedId")@NotNull Long repliedId,
                           @Parameter(name = "content",description = "要回复的站内信内容")@RequestParam("content")@NotNull String content){
        //校验用户状态
        HttpSession session = request.getSession(false);
        User user=(User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //校验要回复的站内信状态
        Message existMessage = messageService.selectById(repliedId);
        if (existMessage==null||existMessage.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
        if (user.getId()==existMessage.getPostUserId()){
            return AppResult.failed("不能回复自己的站内信");
        }
        //构造对象
        Message message=new Message();
        message.setPostUserId(user.getId());
        message.setReceiveUserId(existMessage.getPostUserId());
        message.setContent(content);
        messageService.reply(repliedId,message);
        return AppResult.success();
    }
}
